package com.ycrlkj.android.modulescore.main.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.utils.TextUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.ycrlkj.android.modulescore.main.R;
import com.ycrlkj.android.modulescore.main.apiservice.XxAppointmentApiService;
import com.ycrlkj.android.modulescore.main.databinding.ActivityXxAppointmentAddBinding;
import com.ycrlkj.android.modulescore.main.entity.FreightOrder;
import com.ycrlkj.android.modulescore.main.entity.XxAppointment;
import com.ycrlkj.android.modulescore.main.entity.XxStation;
import com.ycrlkj.android.modulescore.main.viewmodel.XxAppointmentViewModel;
import com.ycrlkj.android.modulescore.main.xpop.PopupBottomSelectFreightOrder;
import com.ycrlkj.android.modulescore.main.xpop.PopupBottomSelectXxStation;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiException;
import com.ycrlkj.android.modulespublic.common.net.exception.ApiExceptionHandler;
import com.ycrlkj.android.modulespublic.common.net.exception.ErrorConsumer;
import com.ycrlkj.android.modulespublic.common.net.transformer.ResponseTransformer;
import com.ycrlkj.android.modulespublic.common.utils.xpopup.XPopupUtil;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_XX_APPOINTMENT_ADD)
public class XxAppointmentAddActivity extends AppCompatActivity {

    @Inject
    XxAppointmentApiService xxAppointmentApiService;

    private ActivityXxAppointmentAddBinding binding;

    private XxAppointmentViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewModel = new ViewModelProvider(this).get(XxAppointmentViewModel.class);
        binding = ActivityXxAppointmentAddBinding.inflate(getLayoutInflater());
        binding.setViewModel(viewModel);
        binding.setLifecycleOwner(this);

        setContentView(binding.getRoot());

        initTitleBar();
    }

    public void initTitleBar() {
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                finish();
            }

            @Override
            public void setBtnRightOnClickListener() {
            }
        });

    }

    public void handleSelectFreightOrder(View view) {
        new XPopup.Builder(this)
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isViewMode(true)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                //.isThreeDrag(true) //是否开启三阶拖拽，如果设置enableDrag(false)则无效
                .asCustom(new PopupBottomSelectFreightOrder(this, new PopupBottomSelectFreightOrder.OnSelectedListener() {
                    @Override
                    public void selected(FreightOrder freightOrder) {
                        viewModel.getFreightOrder().setValue(freightOrder);
                    }
                })/*.enableDrag(false)*/)
                .show();
    }

    public void handleSelectXxStation(View view) {
        new XPopup.Builder(this)
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isViewMode(true)
                .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                //.isThreeDrag(true) //是否开启三阶拖拽，如果设置enableDrag(false)则无效
                .asCustom(new PopupBottomSelectXxStation(this, new PopupBottomSelectXxStation.OnSelectedListener() {
                    @Override
                    public void selected(XxStation xxStation) {
                        viewModel.getXxStation().setValue(xxStation);
                    }
                })/*.enableDrag(false)*/)
                .show();
    }

    public void handleSelectAppointmentTime(View view) {
        TextView tv_appointmentTime = findViewById(R.id.tv_appointmentTime);

        String[] arr = viewModel.getAppointmentTimeArr().getValue();
        Integer defaultChecked = viewModel.getAppointmentTimePosition().getValue();
        XPopupUtil.showSelect(this, "预约时间", arr, defaultChecked, new OnSelectListener() {
            @Override
            public void onSelect(int position, String text) {
                viewModel.getAppointmentTimePosition().setValue(position);
                viewModel.getAppointmentTime().setValue(text);

                tv_appointmentTime.setText(text);
                int color = getResources().getColor(R.color.m_888);
                tv_appointmentTime.setTextColor(color);
                tv_appointmentTime.setTextSize(20);
                tv_appointmentTime.setText(text);
            }
        }, 1200);
    }

    public void handleSave(View view) {

        if (viewModel.getFreightOrder().getValue().getId() == null) {
            ToastUtils.showLong("请选择货运订单");
            return;
        }

        if (viewModel.getXxStation().getValue().getId() == null) {
            ToastUtils.showLong("请选择洗消站点");
            return;
        }

        if (TextUtils.isEmpty(viewModel.getAppointmentTime().getValue())) {
            ToastUtils.showLong("请选择洗消日期");
            return;
        }

        XxAppointment xxAppointment = new XxAppointment();
        xxAppointment.setFreightOrderId(viewModel.getFreightOrder().getValue().getId());
        xxAppointment.setXxStationId(viewModel.getXxStation().getValue().getId());
        xxAppointment.setXxStationName(viewModel.getXxStation().getValue().getName());
        xxAppointment.setVehicleId(viewModel.getFreightOrder().getValue().getVehicleId());
        xxAppointment.setVehiclePlateNum(viewModel.getFreightOrder().getValue().getVehiclePlateNum());
        xxAppointment.setVehiclePlateColor(viewModel.getFreightOrder().getValue().getVehiclePlateColor());
        xxAppointment.setAppointmentTime(viewModel.getAppointmentTime().getValue());

        String userId = SPStaticUtils.getString("userId");
        String userName = SPStaticUtils.getString("userName");

        xxAppointment.setDriverId(userId);
        xxAppointment.setDriverName(userName);

        submit(xxAppointment);
    }

    @SuppressLint("CheckResult")
    public void submit(XxAppointment xxAppointment) {

        LoadingPopupView loadingPopupView = XPopupUtil.showLoading(this);
        XPopupUtil.hideLoadingDelay(loadingPopupView);

        xxAppointmentApiService.add(xxAppointment)
                .compose(ResponseTransformer.obtain())
                .subscribe(resultData -> {
                    ToastUtils.showLong(resultData.getToast());
                    XPopupUtil.hideLoading(loadingPopupView);
                }, new ErrorConsumer() {
                    @Override
                    protected void error(ApiException e) {
                        ApiExceptionHandler.doWith(XxAppointmentAddActivity.this, e);
                    }
                });

    }


}